﻿using Prism.Commands;
using Prism.Mvvm;
using System;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Media.Media3D;
using System.Windows.Shapes;

namespace TimeSolve
{
    /// <summary>
    /// ViewPlane.xaml 的交互逻辑
    /// </summary>
    public partial class SetView : Window
    {
        public SetView(DGraphControl  dGraphControl)
        {
            InitializeComponent();
            this.DataContext = this;
            setViewVM = new SetViewViewModel(this,dGraphControl);
        }

        public  SetViewViewModel setViewVM { get; set; }
        public class SetViewViewModel : BindableBase
        {
            public SetViewViewModel(SetView setViewWin,DGraphControl dGraphControl)
            {
                this.setViewWin = setViewWin;
                this.dGraphControl = dGraphControl;
                setView = new DelegateCommand(setViewF);
                closewin = new DelegateCommand(closewinf);
            }
            private SetView setViewWin { get; set; }
            private DGraphControl dGraphControl { get; set; }
            public DelegateCommand setView { get; set; }
            public DelegateCommand closewin { get; set; }

            private void closewinf()
            {
                setViewWin.Close();
            }
            private void setViewF()
            {
                Vector3D a = new Vector3D();
                Vector3D c = new Vector3D();
                if (setType ==SetViewTypes.坐标轴)
                {
                    a = vup2;
                    c = Vector3D.CrossProduct(vup2, vright2);
                }
                else
                {
                    a = vup1;
                    c = Vector3D.CrossProduct(vup1, vright1);
                }
                if (c.Length == 0)
                {
                    MainViewModel .mainViewModel.log.AddWithTime("需选择两不同的方向");
                }
                else
                {
                    dGraphControl.viewControler.setView(a, c);
                }
            }
  
            public SetViewTypes setType { get => _setType; set { _setType = value; RaisePropertyChanged("setType"); } }
            private SetViewTypes _setType;
            public Vector3D vup1 { get => _vup1; set { _vup1 = value; RaisePropertyChanged("vup1"); } }
            private Vector3D _vup1 = new Vector3D(1, 0, 0);
            public Vector3D vright1 { get => _vright1; set { _vright1 = value; RaisePropertyChanged("vright1"); } }
            private Vector3D _vright1 = new Vector3D(0, 1, 0);
            public Dictionary<Vector3D, string> Axisdic { get => _Axisdic; set { _Axisdic = value; RaisePropertyChanged("Axisdic"); } }
            private Dictionary<Vector3D, string> _Axisdic
                 = new Dictionary<Vector3D, string>()
                 {
                { new Vector3D(1, 0, 0), "X" },
                { new Vector3D(0, 1, 0), "Y" },
                { new Vector3D(0, 0, 1), "Z" },
                { new Vector3D(-1, 0, 0), "-X" },
                { new Vector3D(0, -1, 0), "-Y" },
                { new Vector3D(0, 0, -1), "-Z" }
                 };
            Vector3D vup2 = new Vector3D(1, 0, 0);
            Vector3D vright2 = new Vector3D(0, 1, 0);
            public string Vup2
            {
                get { return string.Format("{0},{1},{2}", vup2.X, vup2.Y, vup2.Z); }
                set
                {
                    double x, y, z;
                    if (VectorCal.Vector3Parse(value, out x, out y, out z))
                    {
                        vup2 = new Vector3D(x, y, z);
                    }
                    RaisePropertyChanged("Vup2");
                }
            }
            public string Vright2
            {
                get { return string.Format("{0},{1},{2}", vright2.X, vright2.Y, vright2.Z); }
                set
                {
                    double x, y, z;
                    if (VectorCal.Vector3Parse(value, out x, out y, out z))
                    {
                        vright2 = new Vector3D(x, y, z);
                    }
                    RaisePropertyChanged("Vright2");
                }
            }
        }

    }

    public enum SetViewTypes
    {
        坐标轴,
        自定义
    }
}
